﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SLWebApp.ChildWindows
{
    public partial class ChildWindow_FlightVesselsUpdate : ChildWindow
    {
        #region properties

        public string UserType
        {
            get
            {
                if (_parent.UserID == 1)
                {
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
        }


        #endregion

        #region cstor


        public ChildWindow_FlightVesselsUpdate()
        {
            InitializeComponent();
        }

     

        public ChildWindow_FlightVesselsUpdate(FlightsVessels parent, ServiceReference_HB.WEB_VM_LINERS model):this()
        {
            _parent = parent;

            InitData(model);
            DataContext = model;
           
        }



        #endregion

        #region private methods

        private void InitData(ServiceReference_HB.WEB_VM_LINERS model)
        {
            ServiceReference_HB.ServiceForSLClient clientService = new ServiceReference_HB.ServiceForSLClient();
            //绑定船名
            clientService.GetAgentIDByUserIDAsync(_parent.UserID);
            clientService.GetAgentIDByUserIDCompleted += (s, e) =>
                {
                    string agentID = e.Result;
                    string strWhere = string.Empty;

                    if (this.UserType == "1")
                    {

                    }
                    else
                    {
                        strWhere = string.Format("VSL_CST_VCM_ID = {0} or VSL_CST_AGT_ID = {0}", agentID);
                    }

                    clientService.GetAllVesselsAsync(strWhere, " VSL_ENNAME ");
                    clientService.GetAllVesselsCompleted += (s2, e2) =>
                        {
                            ObservableCollection<ServiceReference_HB.ST_Vessels> items = e2.Result;
                            cmdEgName.ItemsSource = items;
                            cmdEgName.SelectedItem = (from item in items
                                                      where item.VSL_ENNAME == model.VSL_ENNAME
                                                      select item).FirstOrDefault();
               
                        };

                };



        }


        #endregion

        #region fields

        private FlightsVessels _parent;

        

    

        #endregion


        #region event handler


        private void cmbEngName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                ServiceReference_HB.ST_Vessels item = e.AddedItems[0] as ServiceReference_HB.ST_Vessels;
                txtBoxCNName.Text = item.VSL_CNNAME;
            }
        }

        #endregion

        private void btnSave_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            progressbar.IsBusy = true;
            ServiceReference_HB.WEB_VM_LINERS changedData= DataContext as ServiceReference_HB.WEB_VM_LINERS;
            changedData.LNR_VSL_CD = (cmdEgName.SelectedItem as ServiceReference_HB.ST_Vessels).VSL_CD;
            changedData.VSL_ENNAME = (cmdEgName.SelectedItem as ServiceReference_HB.ST_Vessels).VSL_ENNAME;
            ServiceReference_HB.ServiceForSLClient clientService = new ServiceReference_HB.ServiceForSLClient();
            clientService.IUDForVesselAsync((int)OperationType.Update, _parent.UserID,
              changedData);

          
            clientService.IUDForVesselCompleted += (s1, e1) =>
                {
                    string result = e1.Result;
                    if (result[0] == '1')
                    {
                        this.Close();
                      
                    }
                    else
                    {
                        MessageBox.Show(result.Substring(3));
                        progressbar.IsBusy = false;
                    }
                };

        }


    }



}